package com.wj.jiangan.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.pojo.Emp;
import com.wj.jiangan.pojo.vo.EmpAndPart;
import com.wj.jiangan.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@RestController
@RequestMapping("/emp")
public class EmpController {


    @Autowired
    private IEmpService empService;

    @GetMapping("/{pid}")
    public List<Emp> findEmpByPid(@PathVariable int pid) {
        return empService.findEmpByPid(pid);
    }

    /**
     * @描述 分页查询员工表
     * @创建人 dt
     * @创建时间 2019/11/8
     */
    @GetMapping("/findEmpPage")
    public Map<String, Object> findpageClientNum(Page<Emp> page) {

        IPage<Emp> empByPage = empService.findEmpByPage(page);

        List<Map<String, Object>> list = new ArrayList<>();

        empByPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("eId", o.getEId());
            map.put("eTruename", o.getETruename());
            map.put("eSex", o.getESex());
            map.put("eRemark", o.getERemark());
            list.add(map);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "");
        result.put("count", empByPage.getTotal());
        result.put("data", list);
        return result;
    }

    /**
     * 员工业绩折线图
     *
     * @param pid 部门id
     * @param eid 员工id
     * @return
     */
    @GetMapping("empline")
    public List<Map<String, Object>> getEmpLine(
            @RequestParam(value = "pid", required = false) Integer pid,
            @RequestParam(value = "eid", required = false) Integer eid) {
        return empService.getEmpLineChart(pid, eid);

    }

    @GetMapping("emplist")
    public Map<String, Object> findEmpAndPart(Page page, @RequestParam(required = false) Integer pid) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("pid", pid);
        IPage<EmpAndPart> inpducemp = empService.findEmpAndPartQuery(page, maps);

        List<Map<String, Object>> list = new ArrayList<>();
        inpducemp.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("eId", o.getEmp().getEId());
            map.put("eTruename", o.getEmp().getETruename());
            map.put("eSex", o.getEmp().getESex());
            map.put("eRemark", o.getEmp().getERemark());
//            {{d.eImg}}
            map.put("eImg", "<div><img src=" + o.getEmp().getEImg() + "></div>");
            map.put("ePsw", o.getEmp().getEPsw());

            map.put("pName", o.getPart().getPName());
            String flag = "";
            if (o.getEmp().getEFlag() == 0) {
                flag = "普工";
            } else if (o.getEmp().getEFlag() == 1) {
                flag = "部门主管";
            } else {
                flag = "暂无";
            }
            String admin = "";
            if (o.getEmp().getEAdmin() == 0) {
                admin = "普通操作者";
            } else if (o.getEmp().getEAdmin() == 1) {
                admin = "部门主管";
            } else {
                admin = "暂无";
            }
            map.put("eAdmin", admin);
            map.put("eFlag", flag);
            map.put("eLoginname", o.getEmp().getELoginname());

            list.add(map);
        });

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("count", inpducemp.getTotal());
        result.put("data", list);
        return result;
    }

    @RequestMapping("emplisttest")
    public List<Emp> getEmpById() {
        List<Emp> empById = empService.getEmpById();
        return empById;
    }

   /* @RequestMapping("insertemp")
    public Map<String,Object> insertEmp(Emp emp){
        List<Emp> list = empService.insertEmp(emp);
        Map<String,Object> map = new HashMap<>();
        map.put("eiag","");

        return list;
    }*/
}
